﻿package com.work.androidController;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.domain.AppVersion;
import com.work.domain.CompanyIndexInfo;
import com.work.domain.CompanyInfo;
import com.work.domain.CompanyTopInfo;
import com.work.service.AppVersionService;
import com.work.service.CompanyInfoService;
import com.work.util.Constant;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author zhubingyi
 * @version 1.0
 * @description 安卓客户端接口
 *
 */
@Controller
@RequestMapping("/company")
public class AndroidCompanyInfoController{
	//外网地址
    static private String remoteAddr=Constant.REMOTE_ADDRESS;
    //static private String remoteAddr=Constant.LOCAL_ADDRESS;
    
    //常量：http：//
    static private String http=Constant.HTTP;
	
    
	@Resource
	private CompanyInfoService companyInfoService;
	@Resource
	private AppVersionService appVersionService;
	
	//查询公司首页信息
	@RequestMapping(value="companyIndexList",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String companyIndexList(Model model,AppVersion appv,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			JSONObject ob=new JSONObject();
			//app版本号
			String versionid  = request.getParameter("versionid");
			//String versionid = appv.getVersionid();//此方式暂不兼容
			System.out.println("查询首页获取版本号："+versionid);
			AppVersion appVersion = appVersionService.findLastVersinId();
			//获取app最新版本id
			String findLastVersinId = appVersion.getVersionid();
			//获取app的更新内容
			String updatecontents = appVersion.getUpdatecontents();
			System.out.println("数据库versionid："+findLastVersinId);
			if(!versionid.equals(findLastVersinId)){
				System.out.println("版本不一致！");
				JSONObject data = new JSONObject();
				data.put("updatecontents", updatecontents);
				//code码为1时，说明app需要版本更新；code码为0时，说明版本一直并查询数据成功
				ob.put("code", 1);
				ob.put("data",data);
				ob.put("msg", "检查更新");
			}else{
				System.out.println("版本相同！");
				String contextPath = request.getContextPath();
				String localPort = String.valueOf(request.getLocalPort());
				List<CompanyIndexInfo> list=companyInfoService.findAllCompanyIndexInfo();
				JSONArray jsonArray = null;
				for (CompanyIndexInfo cii : list) {
					String cphoto = cii.getCphoto();
					String concat = remoteAddr.concat(request.getContextPath());
					String cphotoPath =concat.concat(cphoto);
					cii.setCphoto(http.concat(cphotoPath));
					//System.out.println(cii.getCphoto());
				}
				jsonArray = JSONArray.fromObject(list);
				JSONObject data = new JSONObject();
				data.put("workList", jsonArray);
				ob.put("code", 0);
				ob.put("data",data );
				ob.put("msg", "查询成功");
			}
			return ob.toString();
		    
	}
	//查询公司首页推荐位信息
	@RequestMapping(value="companyTopList",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String companyTopList(Model model,AppVersion appv,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			JSONObject ob=new JSONObject();
			//app版本号
			String versionid  = request.getParameter("versionid");
			//String versionid = appv.getVersionid();//此方式暂不兼容
			System.out.println("查询首页获取版本号："+versionid);
			AppVersion appVersion = appVersionService.findLastVersinId();
			//获取app最新版本id
			String findLastVersinId = appVersion.getVersionid();
			//获取app的更新内容
			String updatecontents = appVersion.getUpdatecontents();
			System.out.println("数据库versionid："+findLastVersinId);
			if(!versionid.equals(findLastVersinId)){
				System.out.println("版本不一致！");
				JSONObject data = new JSONObject();
				data.put("updatecontents", updatecontents);
				//code码为1时，说明app需要版本更新；code码为0时，说明版本一直并查询数据成功
				ob.put("code", 1);
				ob.put("data",data);
				ob.put("msg", "检查更新");
			}else{
				String localPort = String.valueOf(request.getLocalPort());
				String contextPath = request.getContextPath();
				List<CompanyTopInfo> list=companyInfoService.findAllCompanyTopIndexInfo();
				JSONArray jsonArray = null;
				for (CompanyTopInfo cii : list) {
					String cphoto = cii.getCphoto();
					String concat = remoteAddr.concat(request.getContextPath());
					String cphotoPath =concat.concat(cphoto);
					cii.setCphoto(http.concat(cphotoPath));
					//System.out.println(cii.getCphoto());
				}
				jsonArray = JSONArray.fromObject(list);
				//System.out.println(jsonArray.toString());
				JSONObject data = new JSONObject();
				    data.put("workTopList", jsonArray);
				    ob.put("code", 0);
				    ob.put("data",data );
				    ob.put("msg", "查询成功");
				//model.addAttribute("companyList", list);
			}
		    return ob.toString();
	}
	
	//查询企业详细信息
	@RequestMapping(value="companyInfoByCid",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String companyInfoByCid(Model model,CompanyInfo ci,AppVersion appv, HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			Integer cid = ci.getCid();
			JSONObject ob=new JSONObject();
			//app版本号
			String versionid  = request.getParameter("versionid");
			//String versionid = appv.getVersionid();//此方式暂不兼容
			System.out.println("查询首页获取版本号："+versionid);
			AppVersion appVersion = appVersionService.findLastVersinId();
			//获取app最新版本id
			String findLastVersinId = appVersion.getVersionid();
			//获取app的更新内容
			String updatecontents = appVersion.getUpdatecontents();
			System.out.println("数据库versionid："+findLastVersinId);
			if(!versionid.equals(findLastVersinId)){
				System.out.println("版本不一致！");
				JSONObject data = new JSONObject();
				data.put("updatecontents", updatecontents);
				//code码为1时，说明app需要版本更新；code码为0时，说明版本一直并查询数据成功
				ob.put("code", 1);
				ob.put("data",data);
				ob.put("msg", "检查更新");
			}else{
				CompanyInfo companyInfo=companyInfoService.findCompanyInfoByCid(cid);
				JSONObject data = new JSONObject();
				    data.put("companyDetail", companyInfo);
				    ob.put("code", 0);
				    ob.put("data",data );
				    ob.put("msg", "查询成功");
			}
		    return ob.toString();
	}
	//查询公司模糊搜索类型
	@RequestMapping(value="companySearchTypeList",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String companySearchTypeList(Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			List<CompanyIndexInfo> list=companyInfoService.findCompanySearchType();
			JSONArray jsonArray = JSONArray.fromObject(list);
			//System.out.println(jsonArray.toString());
			JSONObject data = new JSONObject();
			JSONObject ob=new JSONObject();
			    data.put("workSearchTypeList", jsonArray);
			    ob.put("code", 0);
			    ob.put("data",data );
			    ob.put("msg", "查询成功");
		    return ob.toString();
	}
	//根据条件查询企业列表信息
	@RequestMapping(value="companyListByConditions",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String companyListByConditions(Model model,CompanyInfo companyInfo,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			//获取需查询字段信息
			String type=request.getParameter("type");
			//获取查询内容信息
			String value=new String(request.getParameter("value").getBytes("ISO-8859-1"),"utf-8");
			//System.out.println(type+" "+value);
			List<CompanyIndexInfo> list=companyInfoService.findCompanyListByConditions(type,value);
			JSONArray jsonArray = null;
			for (CompanyIndexInfo cii : list) {
				String cphoto = cii.getCphoto();
				String concat = remoteAddr.concat(request.getContextPath());
				String cphotoPath =concat.concat(cphoto);
				cii.setCphoto(http.concat(cphotoPath));
			}
			jsonArray = JSONArray.fromObject(list);
			JSONObject data = new JSONObject();
			JSONObject ob=new JSONObject();
			    data.put("workList", jsonArray);
			    ob.put("code", 0);
			    ob.put("data",data );
			    ob.put("msg", "查询成功");
		    return ob.toString();
	}
	
   
}
